package com.itheima.test12;

public class Foodie extends Thread{
    private Desk desk;

    public Foodie(Desk desk) {
        this.desk = desk;
    }

    @Override
    public void run() {
        while (true){
            synchronized (desk){
                if (desk.getCount() == 0){
                    break;
                }else {
                    if (desk.isFlag()){
                        //有食物
                        try {
                            desk.wait(100);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                        System.out.println("吃货吃掉了一个食物");
                        desk.setCount(desk.getCount() - 1);
                        desk.setFlag(!desk.isFlag());

                        desk.notifyAll();
                    }else {
                        try {
                            desk.wait();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
        }
    }
}
